package com.csx.saas.platform.system.controller;

import com.csx.basic.annotation.log.SysLog;
import com.csx.basic.base.R;
import com.csx.basic.base.controller.SuperController;
import com.csx.saas.platform.system.dto.SaveUserRoleDTO;
import com.csx.saas.platform.system.dto.SystemUserRolePageQuery;
import com.csx.saas.platform.system.dto.SystemUserRoleSaveDTO;
import com.csx.saas.platform.system.dto.SystemUserRoleUpdateDTO;
import com.csx.saas.platform.system.entity.SystemUserRole;
import com.csx.saas.platform.system.service.SystemUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 角色分配
 * 账号角色绑定
 * </p>
 *
 * @author ChenSongXin
 * @date 2021-02-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/systemUserRole")
@Api(value = "SystemUserRole", tags = "角色分配")
//@PreAuth(replace = "platform:systemUserRole:")
public class SystemUserRoleController extends SuperController<SystemUserRoleService, Long, SystemUserRole, SystemUserRolePageQuery, SystemUserRoleSaveDTO, SystemUserRoleUpdateDTO> {

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<SystemUserRole> systemUserRoleList = list.stream().map((map) -> {
            SystemUserRole systemUserRole = SystemUserRole.builder().build();
            //TODO 请在这里完成转换
            return systemUserRole;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(systemUserRoleList));
    }

    @ApiOperation(value = "保存授权角色", notes = "保存授权角色")
    @PostMapping("/save/userRole")
    @SysLog("保存授权角色")
    public R<Boolean> saveUserRole(@RequestBody SaveUserRoleDTO saveUserRoleDTO) {
        baseService.saveUserRole(saveUserRoleDTO);
        return R.success();
    }
}
